package com.itheima.controller;

import com.itheima.dto.AnimalDTO;
import com.itheima.entity.Animal;
import com.itheima.entity.User;
import com.itheima.service.AnimalService;
import com.itheima.service.UserService;
import com.itheima.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/animals")
public class AnimalController {

    @Autowired
    private AnimalService animalService;

    @Autowired
    private UserService userService;

    @Autowired
    private JwtUtil jwtUtil;

    // 添加动物信息
    @PostMapping
    public ResponseEntity<Animal> addAnimal(@RequestBody Animal animal) {
        animal.printAnimalDetails();
        return ResponseEntity.status(201).body(animalService.addAnimal(animal));
    }

    // 获取所有动物信息
    @GetMapping
    public ResponseEntity<List<Animal>> getAllAnimals(@RequestHeader("Authorization") String token) {
        // 从 Authorization 头中获取 Token
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }

        // 获取当前用户
        String username = jwtUtil.extractUsername(token);
        User user = userService.findByUsername(username).orElse(null);

        if (user.getIsSuper()) {
            // 如果是管理员，返回所有动物信息
            return ResponseEntity.ok(animalService.getAllAnimals());
        } else {
            // 如果是普通用户，返回已审核的动物信息
            return ResponseEntity.ok(animalService.getApprovedAnimals());
        }
    }

    // 搜索动物
    @GetMapping("/search")
    public ResponseEntity<List<Animal>> searchAnimals(@RequestParam(required = false) String species,
                                                      @RequestParam(required = false) String size,
                                                      @RequestParam(required = false) String color,
                                                      @RequestParam(required = false) String location,
                                                      @RequestHeader("Authorization") String token) {
        // 从 Authorization 头中获取 Token
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }

        // 获取当前用户
        String username = jwtUtil.extractUsername(token);
        User user = userService.findByUsername(username).orElse(null);

        if (user.getIsSuper()) {
            // 如果是管理员，返回所有符合动物信息
            return ResponseEntity.ok(animalService.searchAnimals(species, size, color, location));
        } else {
            // 如果是普通用户，返回已审核符合的动物信息
            return ResponseEntity.ok(animalService.searchApproveAnimals(species, size, color, location));
        }

    }
    // 更新动物信息
    @PutMapping("/{id}")
    public ResponseEntity<Animal> updateAnimal(@PathVariable Long id,
                                               @RequestBody AnimalDTO animalDTO,
                                               @RequestHeader("Authorization") String token) {
        // 从 Authorization 头中获取 Token
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }

        // 获取当前用户
        String username = jwtUtil.extractUsername(token);
        User user = userService.findByUsername(username).orElse(null);

        if (user.getIsSuper()) {
            // 如果是管理员，返回所有符合动物信息
            return ResponseEntity.ok(animalService.updateAnimal(id, animalDTO));
        } else {
            // 如果是普通用户，返回已审核符合的动物信息
            return ResponseEntity.ok(null);
        }

    }

    // 删除动物信息
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteAnimal(@PathVariable Long id,
                                             @RequestHeader("Authorization") String token) {
        // 从 Authorization 头中获取 Token
        if (token.startsWith("Bearer ")) {
            token = token.substring(7);
        }

        // 获取当前用户
        String username = jwtUtil.extractUsername(token);
        User user = userService.findByUsername(username).orElse(null);

        if (user.getIsSuper()) {
            // 如果是管理员，返回所有符合动物信息
            animalService.deleteAnimal(id);
            return ResponseEntity.noContent().build();
        } else {
            // 如果是普通用户，返回已审核符合的动物信息
            return ResponseEntity.ok(null);
        }

    }
}
